//
//  NetWorkTool.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/14.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkTool.h"

#import "LoginViewController.h"

@implementation NetWorkTool

//post请求 不带token
+(void)requestPostWithUrl:(NSString *)urlString WithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock withFailedBlock:(void(^)(NSString *errorStr))failedBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        failedBlock(@"网络错误");
    }];
}

//post请求 携带token
+(void)requestTokenPostWithUrl:(NSString *)urlString WithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock withFailedBlock:(void(^)(NSString *errorStr))failedBlock{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:urlString parameters:params headers:[NetWorkTool getUserToken] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] == 500400) {
            [[XNProgressHUD shared] showErrorWithTitle:@"请重新登录"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Configuration loginClick];
            });
            return;
        }
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        failedBlock(@"网络错误");
    }];
}

//get请求 不带token
+(void)requestGetWithUrl:(NSString *)urlString withSuccessBlock:(void(^)(id responseObj))successBlock withFailedBlock:(void(^)(NSString *errorStr))failedBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager GET:urlString parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        failedBlock(@"网络错误");
    }];
}

//get请求 携带token
+(void)requestTokenGetWithUrl:(NSString *)urlString withSuccessBlock:(void(^)(id responseObj))successBlock withFailedBlock:(void(^)(NSString *errorStr))failedBlock{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager GET:urlString parameters:nil headers:[NetWorkTool getUserToken] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] intValue] == 500400) {
            [[XNProgressHUD shared] showErrorWithTitle:@"请重新登录"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [Configuration loginClick];
            });
            return;
        }
        if ([responseObject[@"code"] intValue] != 200) {
            [[XNProgressHUD shared] showErrorWithTitle:responseObject[@"msg"]];
            return;
        }
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error.code  == -1001) {
            [[XNProgressHUD shared] dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[XNProgressHUD shared] showErrorWithTitle:@"请求超时,请刷新后再试！"];
            });
            return;
        }
        failedBlock(@"网络错误");
    }];
}

+(NSDictionary *)getUserToken{
    if ([[UserInfomationManager shareManager].token isEmpty] == NO && [UserInfomationManager shareManager].token.length > 0) {
        return @{@"Authorization":[UserInfomationManager shareManager].token};
    }
    return @{@"Authorization":@""};
}

@end
